<?php

/**
 * docente actions.
 *
 * @package    ecampus
 * @subpackage docente
 * @author     Grupo 608
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class docenteActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $this->redirect('usuario/index');
  }

//  public function executeNuevaPublicacionCurso(sfWebRequest $request)
//  {
//      $this->form = new PublicacionCursoForm();
//
//  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new DocenteForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new DocenteForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    
    $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario'))), sprintf('Object docente does not exist (%s).', $request->getParameter('id_usuario')));
//    $this->form = new DocenteForm($docente);
    $this->form = new EditarDocenteForm($docente);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario'))), sprintf('Object docente does not exist (%s).', $request->getParameter('id_usuario')));
   // $this->form = new DocenteForm($docente);
    $this->form = new EditarDocenteForm($docente);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario'))), sprintf('Object docente does not exist (%s).', $request->getParameter('id_usuario')));
    $docente->delete();

    $this->redirect('docente/index');
  }

  public function executeDatos(sfWebRequest $request)
  {
    $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario'))), sprintf('Object docente does not exist (%s).', $request->getParameter('id_usuario')));
    $this->datos = new DocenteForm($docente);
    $this->id_usuario = $request->getParameter('id_usuario');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $docente = $form->save();
//      $this->redirect('docente/edit?id_usuario='.$docente->getIdUsuario());
      $this->forward('docente', 'index');
    }
  }
  public function executeCursos(sfWebRequest $request)
  {
      if(!is_null($request->getParameter('id_usuario')))
      {
         $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
         $this->cursos = $this->docente->getCursos();
      }
      else{
          $this->redirect('homepage');
      }
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($this->docente);
  }
  public function executeAlertas(sfWebRequest $request)
  {
      $this->borrar = $request->getParameter('borrar');
      
      $this->id_usuario = $request->getParameter('id_usuario');
      $this->marcar_leido = $request->getParameter('marcar_leido');
      $this->tipoUsuario = 'docente';

      $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
      $this->alertas = Doctrine_Core::getTable('Usuario')->getAlertas($request->getParameter('id_usuario'));
      $this->alertasNoLeidas = Doctrine_Core::getTable('Usuario')->getAlertasNoLeidas($request->getParameter('id_usuario'));
  }
  public function executeTutorias(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');
      

//      $this->tutorias = Doctrine_Core::getTable('Tutoria')->findBy('id_docente', $this->id_docente);
//      $this->alumnosTutorias = $this->tutorias;

//      $this->alumnosTutorias = Doctrine_Core::getTable('Tutoria')->findByDql($dql, $params);
      if(!is_null($this->id_usuario))
      {
          $this->docente = Doctrine_Core::getTable('Docente')->find(array($request->getParameter('id_usuario')));
          $this->alumnosTutorias = $this->docente->getAlumnosTutorias();
      }
  }
  public function executeRegistrosTutorias(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');
      $this->tutoria = Doctrine_Query::create()
                ->select()
                ->from('tutoria t')
                ->where('t.id_alumno = ?',$request->getParameter('id_alumno'))
                      ->fetchOne();

//        $this->tutoria = Doctrine::getTable('Tutoria')->findBy('id_alumno',$request->getParameter('id_alumno'));
        //$id_docente = $this->tutoria->
        $this->docente = Doctrine_Core::getTable('docente')->find($this->tutoria->getIdDocente());
      //$this->docente = Doctrine::getTable('Docente')->find();
//      $this->registrosTutorias = Doctrine_Core::getTable('RegistroTutoria')->findBy('id_tutoria', $request->getParameter('id_tutoria'));
      $this->registrosTutorias = Doctrine_Core::getTable('RegistroTutoria')->getHistorialDeAlumno($request->getParameter('id_alumno'));
  }
  public function executeMensajes(sfWebRequest $request)
  {
      $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
      $this->mensajes = MensajeTable::getMensajesDeUsuario($request->getParameter('id_usuario'));
  }
  public function executeReservas(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');
      $this->docente = Doctrine_Core::getTable('Docente')->find($request->getParameter('id_usuario')); //->getCursos();
  }
  public function executeRecursos(sfWebRequest $request)
  {
      $this->docente = Doctrine_Core::getTable('Docente')->find($request->getParameter('id_usuario'));
      $this->recursos = Doctrine::getTable('Recurso')
          ->createQuery('a')
          ->execute();
  }
  public function executeNuevaReserva(sfWebRequest $request)
  {
      $this->id_recurso = $request->getParameter('id_recurso');
      $this->id_docente = $request->getParameter('id_usuario');

      $this->docente = Doctrine_Core::getTable('Docente')->find($request->getParameter('id_usuario'));
      $this->form = new ReservaderecursoForm();

      
//      $this->form->configurarRecurso($id_recurso);
//        $this->form->configurarCursos($docente);
//        
//      $this->form->widgetSchema['id_curso'] = new sfWidgetFormChoice(array(
//            'choices' => array(1,2),
//            'expanded' => false,
//            'label' => 'Curso',
//      ) );
  }
  public function executeArchivos(sfWebRequest $request)
  {
     $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
     $this->cursos = $this->docente->getCursos();
  }

  public function executeAlumnos(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');

      $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_alumno')));
      $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
      $this->curso = Doctrine::getTable('Curso')->find(array($request->getParameter('id_curso')));
  }
  public function executeFechasAlumnoTutoria(sfWebRequest $request)
  {
      $this->docente = Doctrine::getTable('Docente')->find(array($request->getParameter('id_usuario')));
      $this->id_usuario = $request->getParameter('id_usuario');
      $this->id_tutoria = $request->getParameter('id_tutoria');

      $this->fechasAlumnoTutoria = Doctrine_Query::create()
              ->select('ft.fecha fecha')
              ->from('fechastutoria ft')
              ->where('ft.id_tutoria = ?',$this->id_tutoria)
//                      ->andWhere('ft.fecha')
                      ->orderBy('fecha ASC')
                      ->fetchArray()
              ;
  }
}